package com.zfjt.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ReturnBean;
import com.zfjt.entity.UserApplyBean;
import com.zfjt.entity.UserBean;
import com.zfjt.packnum.PackNum00004;
import com.zfjt.service.BrabchService;
import com.zfjt.service.UserApplyService;
import com.zfjt.service.UserService;

/**
 * 注册用户审核接口
 * @author xk
 * @version 1.0
 * @date 2017年3月29日
 */

@Controller
public class AddUserController {
	
	private ReturnBean mReturn;
	
	private Map<String, String> fhdata;
	
	private String data;
	
	@Autowired
	private UserApplyService mUserApply;
	
	@Autowired
	private UserService mUser;
	
	@Autowired
	private BrabchService mBrabch;
	 
	@RequestMapping(value="00004")
	@ResponseBody
	public ReturnBean AddUser(HttpSession hseesion){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		data =  (String) hseesion.getAttribute("data");
		PackNum00004 no00004 = JSON.parseObject(data,PackNum00004.class);
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {
			mReturn.setPortnum("00004");
			mReturn.setCode("001");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{
			String account = no00004.getAccount();
			String type = no00004.getType();
			UserApplyBean UserApply = mUserApply.selectByPrimaryKey(account);
			//删除这条数据把其移倒用户表里
			mUserApply.deleteByPrimaryKey(account);
			if (no00004.getResults().equals("1")) {
				UserBean user = new UserBean();
				user.setUserId(UUID.randomUUID().toString());
				user.setAccount(UserApply.getAccount());
				user.setBranch(UserApply.getBranch());
				user.setMobile(UserApply.getAccount());
				user.setName(UserApply.getName());
				user.setPassword(UserApply.getPassword());
				user.setNews(UserApply.getPhoneNum());
				user.setType(type);
				user.setHead("");
				user.setMoney("0");
				user.setText("0");
				user.setYuliu(mBrabch.selectByPrimaryKey(UserApply.getBranch()).getBrabchName());
				mUser.insert(user);
				mReturn.setCode("000");
				fhdata.put("jieguo", "agree");
				String fh = JSON.toJSONString(fhdata);
				mReturn.setData(fh);
				mReturn.setPortnum("00004");
			}else if(no00004.getResults().equals("2")){
				mReturn.setCode("000");
				fhdata.put("jieguo", "turndown");
				mReturn.setData(JSON.toJSONString(fhdata));
				mReturn.setPortnum("00004");
			}
		}
		return mReturn;
	}
}
